package com.rong.juc_test.juc_base.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步回调
 */
public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 异步回调 无返回值
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + " 异步无返回值");
        });
        completableFuture.get();
        // 异步回调 有返回值
        CompletableFuture<Integer> c2 = CompletableFuture.supplyAsync(() ->{
            System.out.println(Thread.currentThread().getName() + " 异步有返回值");
            int i = 1/0;
            return 1024;
        });
        c2.whenComplete((t,u) ->{
            System.out.println("异步有返回值结果：t=" + t); // 返回值
            System.out.println("异步有返回值结果：u=" + u); // 异常信息
        }).get();

    }
}
